<?php


namespace app\admin\controller\general;

use app\common\controller\Backend;
use think\facade\Db;
class Template extends Backend
{
    protected $model = null;
    public function initialize(){
        parent::initialize();
        $this -> model = Db::name('template_message');
    }
    public function index(){
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where('status', 1)
                ->count();
            $list = $this->model
                ->where($where)
                ->where('status', 1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = ['total' => $total, 'rows' => $list];
            return json($result);
        }
        return $this-> fetch();
    }

    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if($params){
                $params['createtime'] = time();
                $result =  $this->model -> insert($params);
//                $result = $this->model->save($params);
                if ($result) {
                    $this->success();
                } else {
                    $this->error();
                }
            }
            $this->error();
        }
        return $this->fetch();
    }


    public function edit($ids=null){
        $row = $this->model->find($ids);
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if($params){
                $params['updatetime'] = time();
                $this->model -> where('id',$ids) -> update($params);
            }
            $this->success();
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

}